package Thread.Demo1;

import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;

/**
 * @Author wangzhipeng
 * @Date 2020-08-09 9:05 下午
 * @Description 练习Thread，实现多线程同步下载图片
 *
 */
public class TestThread2 extends Thread {

    private String url; //网络图片地址
    private String name; //保存的文件名

    public TestThread2(String url, String name) {
        this.url = url;
        this.name = name;
    }

    @Override
    public void run() {
        WebDownLoader webDownLoader = new WebDownLoader();
        webDownLoader.downloader(url,name);
        System.out.println("下载了文件名为"+name);
    }


    public static void main(String[] args) {
        TestThread2 thread = new TestThread2("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1596989026817&di=a8bac00f5e24cfc77c741037f0df65ac&imgtype=0&src=http%3A%2F%2Fn.sinaimg.cn%2Fsinacn02%2F350%2Fw628h522%2F20180720%2F9caa-hfnsvzc1454231.jpg","yangchaoyue.jpg");
        thread.start();
    }
}

class WebDownLoader{
    //下载方法
    public void downloader(String url,String name){
        try {
            FileUtils.copyURLToFile(new URL(url),new File(name));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("IO异常，downloader方法出现问题");
        }
    }
}
